/*

 * 数据字典管理

 */

Ext.onReady(function(){

	

	Ext.QuickTips.init();

	//树节点ID 树节点名称

	var treeWin_docId="0000", treeWin_docSubject="权限ROOT", v_start=0, v_limit=20;

	var titleVar = "权限字典";

    var sysDataBooleanStore = new Ext.data.JsonStore({
	    xtype:'jsonstore',
		url: i1510.contextPath + '/admin/sysData/listExt.do?docStatus=10&fdParentCode=BOOLEAN',
	    root: 'root',
	    totalProperty: 'total',
	    fields: ['fdCode','docSubject'],
		autoLoad:true
	});


	var mapObj = [

		{ name:'docId', type:'string'},

		{ name:'docSubject', type:'string'},

		{ name:'url', type:'string'},

		{ name:'javaClass', type:'string'},

		{ name:'parentId', type:'string'},

		{ name:'parentName', type:'string'},

		{ name:'javaMethod', type:'string'},

		{ name:'fdOrder', type:'string'},

		{ name:'icon', type:'string'},

		{ name:'isValid', type:'string'},

		{ name:'isMenu', type:'string'},

		{ name:'descs', type:'string'},

		{ name:'docStatus', type:'string'}

	];

	

	//listExt数据

	var listStore = new Ext.data.JsonStore({

	    url: i1510.contextPath+'/admin/sysPop/listExt.do',

	    root: 'root',

	    totalProperty: 'total',

	    fields: mapObj,

	    listeners:{beforeload:function(a){a.baseParams={start:v_start, limit:v_limit,parentId:treeWin_docId,docStatus:""};}}

	});
	//excel表单
	var excelForm = new Ext.FormPanel({
		layout : 'form',
		frame:true,
		labelWidth:60,
		border : false,
		fileUpload: true, 
		method:'POST', 
		enctype:'multipart/form-data', 
		padding : '15 10 0 8',
		defaults : {
			anchor : '100%'
		},
		items:[{
			xtype:"fieldset",
			title:"选择文件",
			padding:'20',
			layout:"column",
			defaults:{
				xtype:"container",
				autoEl:"div",
				labelAlign:'right',
				layout:"form"
			},
			items:[{
				columnWidth:.7,
				items:[{
					xtype : 'textfield',
					inputType : 'file',//文件类型
					fieldLabel : '文件地址',
					name : 'file',
					anchor : '95%'
				}]
			}]
		}]

	});
	//上传Excel窗口
    var excelWin = new Ext.Window({
		title : '上传Excel',
		width:550,
		height:220,
		closeAction:'hide',
		modal : true,
		resizable : false,
		layout : 'fit',
		buttonAlign : 'center',
		items : [excelForm],
		buttons : [{
			text : '上传',
			handler : function() {

				var f = excelForm.getForm();
				var files = f.findField("file").getValue();
				if(files.substring(files.length-4,files.length)!=".xls"){
					Ext.Msg.alert("信息提示","请选择已 .xls 结尾的文件");
					return ;
				}
				if (files) {
					excelForm.getForm().submit({
					    url: i1510.contextPath+"/admin/sysPop/uploadExt.do",
						success : function(form, rtn) {
								Ext.Msg.alert('信息提示','上传保存成功.');
								excelForm.getForm().reset();
						   		excelWin.hide();
						   		listStore.reload();
						   		tree.root.reload();
						},
						failure : function(form, rtn) {
							Ext.Msg.alert('信息提示','连接失败.'+rtn.result.message);
						},
						waitTitle : '提交',
						waitMsg : '正在保存数据，稍后...'
					}); 
					
				}else{
					Ext.Msg.alert('信息提示','文件不能为空');
				}
			
				excelWin.hide();
			}
		}, {
			text : '取消',
			handler : function() {
				excelWin.hide();
			}

		}]

	});	

	//组织列表

    var grid = new Ext.grid.GridPanel({

        store: listStore,

        cm: new Ext.grid.ColumnModel({

			defaults: {	menuDisabled : true},//禁止表头菜单

			columns:[new Ext.grid.RowNumberer(),

				{header: 'ID', width: 100, sortable:true, dataIndex: 'docId',hidden:true},

	            {header: '排序', width: 130, sortable:true, dataIndex: 'fdOrder'},

	            {header: '名称', width: 130, sortable:true, dataIndex: 'docSubject'},

	            {header: '链接', width: 80, sortable:true, dataIndex: 'url'},

	            {header: '类', width: 60, sortable:true, dataIndex: 'javaClass'},

	            {header: '方法', width: 60, sortable:true, dataIndex: 'javaMethod'},

	            {header: '上级ID', width: 60, sortable:true, dataIndex: 'parentId',hidden:true},

	            {header: '上级名称', width: 60, sortable:true, dataIndex: 'parentName'},

	            {header: '是否菜单', width: 60, sortable:true, dataIndex: 'isMenu',renderer:fn_booleanExt},

	            {header: '是否校验', width: 60, sortable:true, dataIndex: 'isValid',renderer:fn_booleanExt},
	            
	            {header: '图标代码', width: 60, sortable:true, dataIndex: 'icon'},

	            {header: '状态', width: 60, sortable:true, dataIndex: 'docStatus',renderer:fn_docStatusExt}

	        ]

        }),

        stripeRows: true, 	//行分隔符

        columnLines : true, //列分隔符

		frame:true,

		region:'center',

		title:titleVar+'信息',

        iconCls:'menu-53',

        

        tbar:[{

        	text:'增加',

        	iconCls:'btn-add',

        	handler: function(){

        		fn_add_baseWin();

        	}

        },'-',{

        	text:'修改',

        	iconCls:'btn-edit',

        	handler: function(){

        		fn_update_baseWinList();

        	}

        }, '-', {

			text : '激活',

			iconCls : 'clock',

			handler : function() {

				fn_del_baseWinList("10");

			}

		},'-',{

        	text:'删除',

        	iconCls:'btn-remove',

        	handler: function(){

        		fn_del_baseWinList00("00");

        	}

        },'->',{
        	text:'导入',
        	iconCls:'menu-12',
        	handler: function(){
        		fn_upload_baseWin();
        	}

        },'-',{
        	text:'下载',
        	iconCls:'menu-11',
        	handler: function(){
        		fn_download_baseWin();
        	}

        }],

        

        bbar: new Ext.PagingToolbar({

            pageSize: v_limit,

            store: listStore,

            displayInfo: true

        }),

        

        listeners:{

        	rowdblclick:function(){

        		fn_update_baseWinList();

        	}

        }

    });
    //导入数据baseWin
	function fn_upload_baseWin(){

		excelWin.show();
		excelForm.getForm().reset();
	}
    //下载模版baseWin
	function fn_download_baseWin(){
		
		excelWin.show();
		excelForm.getForm().reset();
	}
	
	//新增baseWin
	function fn_add_baseWin(){

		baseWin.show();

		baseForm.getForm().reset();

		baseForm.getForm().findField("parentId").setValue(treeWin_docId);

		baseForm.getForm().findField("parentName").setValue(treeWin_docSubject);

	}

	//修改baseWin

	function fn_update_baseWinList(){

		var record= grid.getSelectionModel().getSelected(); 

		if(!record){

        	Ext.Msg.alert('信息提示','请选择要修改的记录');

		}else{

			baseWin.show();

			baseForm.getForm().loadRecord(record);

		}

	}

	//list中激活baseWin
	function fn_del_baseWinList(docStatus){
		var record= grid.getSelectionModel().getSelected();
		if(!record){
        	Ext.Msg.alert('信息提示','请选择要激活的记录');  
		}else{
			Ext.MessageBox.confirm('激活提示', '是否激活该记录？', function(c) {
			   if(c=='yes'){
			   		Ext.Ajax.request({
			   			url : i1510.contextPath+'/admin/sysPop/updateStatus.do',
			   			params:{  docId : record.get("docId"),docStatus:docStatus, },
			   			success : function(rtn) {
			   				var temp = Ext.util.JSON.decode(rtn.responseText);
			   				if(temp.code =="0"){
			   					Ext.Msg.alert("信息提示",temp.messages);
			   				}
			   				listStore.reload();
			   			}
			   		});
			    }
			});
		}
	}
	//list中删除baseWin

	function fn_del_baseWinList00(docStatus){

		var record= grid.getSelectionModel().getSelected();

		if(!record){

        	Ext.Msg.alert('信息提示','请选择要删除的记录');  

		}else{

			Ext.MessageBox.confirm('删除提示', '是否删除该记录？', function(c) {

			   if(c=='yes'){

			   		Ext.Ajax.request({

			   			url : i1510.contextPath+'/admin/sysPop/updateStatus00.do',

			   			params:{  docId : record.get("docId"),docStatus:docStatus, },

			   			success : function(rtn) {

			   				var temp = Ext.util.JSON.decode(rtn.responseText);

			   				if(temp.code =="0"){

			   					Ext.Msg.alert("信息提示","该类别下有信息不能删除");

			   				}

			   				listStore.reload();

			   			}

			   		});

			    }

			});

		}

	}

	//tree中删除baseWin

	function fn_del_baseWin(){

		

		if(treeWin_docId){

    		var node = tree.getNodeById(treeWin_docId);

    		var pnode = node.parentNode;

			Ext.MessageBox.confirm('删除提示', '是否删除"'+node.text+'"类别？', function(c) {

			   if(c=='yes'){

			   		Ext.Ajax.request({

			   			url : i1510.contextPath+'/admin/sysPop/updateStatus.do',

			   			params:{ docId : treeWin_docId,docStatus:"00" },

			   			success : function(rtn) {

			   				var temp = Ext.util.JSON.decode(rtn.responseText);

			   				if(temp.code =="0"){

			   					Ext.Msg.alert("信息提示","该类别下有信息不能删除");

			   				}else{

				   				treeWin_docId = "0000";     //设为默认节点

				   				treeWin_docSubject = "";

				   				tree.buttons[1].setDisabled(true);  //禁用删除按钮

				   				pnode.removeChild(node);	//删除节点

				   				if(pnode.childNodes.length==0){	//如果无子节点则修改属性

				   					pnode.leaf = true;

				   				}

				   				

			   				}

			   			}

			   		});

			    }

			});

		}

	}

	//类别树

	var tree = new Ext.tree.TreePanel({

		title:'类别',

		region : 'west',

		width : 180,

        minSize: 150,

        maxSize: 300,

        split : true,

		useArrows: true,

        autoScroll:true,

        animate:true,

        enableDD:false,

        containerScroll: true,

        frame:true,

        dataUrl: i1510.contextPath+'/admin/sysPop/findTree.do',

        root: {

            nodeType: 'async',

            text: '所有信息',

            draggable: false,

            id: '0000'

        },

        buttonAlign : 'left',

        buttons:[{

        	text:'新增信息',

        	handler:function(){

        		fn_add_baseWin();

        	}

        },{

        	text:'删除信息',

        	disabled:true,

        	handler:function(){

        		fn_del_baseWin();

        	}

        }],

        listeners:{

        	click:function(n){

        		//获取点击后的树节点ID 和 text

        		treeWin_docId = n.id;

        		treeWin_docSubject = n.text;

        		//设置删除按钮状态 下级不能有子节点

        		if(!n.leaf||treeWin_docId=="0000"){

        			tree.buttons[1].setDisabled(true);

        		}else{

        			tree.buttons[1].setDisabled(false);

        		}

			   	//刷新list

			   	listStore.reload();

        	}

        }

	});

	

	//展开节点

	tree.getRootNode().expand();

	

	//组织类别表单

	var baseForm = new Ext.FormPanel({

		layout : 'form',

		frame:true,

		labelWidth:60,

		border : false,

		padding : '15 10 0 8',

		defaults : {

			anchor : '100%'

		},

		items:[{

			xtype:'textfield',

			anchor : '100%',

			name:'docSubject',

			fieldLabel:'组织名称',

			allowBlank : false,

			maxLength :36

		},{

			xtype:'numberfield',

			anchor : '100%',

			name:'fdOrder',

			fieldLabel:'排序',

			allowBlank : false,

			maxLength :4

		},{
			xtype:'combo',
			hiddenName:'isValid',//如果不设置就会提交displayField的值
			name:'isValid',
			fieldLabel:'是否校验',
			mode: 'local',
			maxLength :10,
			triggerAction: 'all',
			valueField :'fdCode',
			displayField: 'docSubject',
			emptyText: '请选择',
			editable : false,
			store : sysDataBooleanStore
		},{
			xtype:'combo',
			hiddenName:'isMenu',//如果不设置就会提交displayField的值
			name:'isMenu',
			fieldLabel:'是否菜单',
			mode: 'local',
			maxLength :10,
			triggerAction: 'all',
			valueField :'fdCode',
			displayField: 'docSubject',
			emptyText: '请选择',
			editable : false,
			store : sysDataBooleanStore
		},{

			xtype:'textfield',

			anchor : '100%',

			name:'url',

			fieldLabel:'链接',

			allowBlank : true,

			maxLength :200

		},{

			xtype:'textfield',

			anchor : '100%',

			name:'javaClass',

			fieldLabel:'类',

			allowBlank : true,

			maxLength :200

		},{

			xtype:'textfield',

			anchor : '100%',

			name:'javaMethod',

			fieldLabel:'方法',

			allowBlank : true,

			maxLength :200

		},{

			xtype:'textfield',

			anchor : '100%',

			name:'icon',

			fieldLabel:'图标代码',

			allowBlank : true,

			maxLength :200

		},{

			xtype:'textfield',

			anchor : '100%',

			name:'parentName',

			fieldLabel:'上级名称',

			allowBlank : true,

			readOnly:true,

			maxLength :36

		},{

			xtype:'textarea',

			name:'descs',

			fieldLabel:'描&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;述',

			height:100,

			maxLength :100

		},{

			xtype : 'hidden',

		    name : 'docId'

		},{

			xtype : 'hidden',

		    name : 'parentId'

		}]

	});

 

	//提交baseWin

	var fn_sumbit_baseWin = function(next){



		if (baseForm.getForm().isValid()) {

			baseForm.getForm().submit({

			    url: i1510.contextPath+'/admin/sysPop/save.do',

				success : function(form, rtn) {

					

					var id = rtn.result.docId;


					var existNode = tree.getNodeById(id);

					if(existNode != null){

						existNode.text = form.findField("docSubject").getValue();

						//更新树跟节点
						tree.root.reload();

					}else{

						//创建新节点

						var node = new Ext.tree.TreeNode({

							id:id,

							text:form.findField("docSubject").getValue(),

							iconCls:'menu-folder',

							leaf:true

						});

						//修改父节点

						var pnode = tree.getNodeById(treeWin_docId);

						pnode.appendChild(node);

						pnode.leaf=false;

						pnode.expand();

					}

					

				   	if(next){

				   		baseForm.getForm().reset();

						baseForm.getForm().findField("parentId").setValue(treeWin_docId);

						baseForm.getForm().findField("parentName").setValue(treeWin_docSubject);

					}else{

						baseWin.hide();

					}

				   	//刷新list

				   	listStore.reload();

				},

				failure : function(form, rtn) {

					Ext.Msg.alert('信息提示','连接失败.'+rtn.result.message);

				},

				waitTitle : '提交',

				waitMsg : '正在保存数据，稍后...'

			});

		}

	}

	//增加组织类别窗口

    var baseWin = new Ext.Window({

		title : '新增/修改',

		width:350,

		height:350,

		closeAction:'hide',

		modal : true,

		resizable : false,

		layout : 'fit',

		buttonAlign : 'center',

		items : [baseForm],

		buttons : [{

			text : '保存并新增',

			handler : function() {

				fn_sumbit_baseWin(true);

			}

		},{

			text : '保存',

			handler : function() {

				fn_sumbit_baseWin(false);

			}

		}, {

			text : '取消',

			handler : function() {

				baseWin.hide();

			}

		}]

	});

    

	//布局

    new Ext.Viewport({

		layout:'fit',

		items:[{

			frame:true,

			layout:'border',

			items:[tree,grid]

		}]

	});



});

